package com.idroid.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.idroid.R;
import com.idroid.domain.AreaType;
import com.idroid.domain.ParkingArea;
import com.idroid.fragments.SearchFragment;

public class FilterDialog extends DialogFragment {

	private SearchFragment searchFragment;
	Spinner areaTypeSpinner;
	EditText maxPriceEditText;

	public static FilterDialog newInstance(SearchFragment searchFragment) {

		FilterDialog dialog = new FilterDialog();
		dialog.searchFragment = searchFragment;

		return dialog;
	}

	public View createDialogView() {
		View v = searchFragment.getActivity().getLayoutInflater()
				.inflate(R.layout.filter_dialog, null);

		areaTypeSpinner = (Spinner) v.findViewById(R.id.areatypespinner);
		ArrayAdapter<AreaType> adapter = new ArrayAdapter<AreaType>(
				this.getActivity(), android.R.layout.simple_spinner_item,
				AreaType.getAreaTypes());

		areaTypeSpinner.setAdapter(adapter);
		areaTypeSpinner.setSelection(AreaType.NoInformation.ordinal());

		maxPriceEditText = (EditText) v.findViewById(R.id.maxPriceEditText);

		return v;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final View v = createDialogView();

		return new AlertDialog.Builder(getActivity()).setTitle("Set Filters:")
				.setPositiveButton("Cancel", new OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						searchFragment.getFilterCheckBox().setChecked(false);
						searchFragment.setMaxPriceFilter(false, 0.0);
						searchFragment.setAreaTypeFilter(false,
								AreaType.NoInformation);

					}
				}).setView(v).setNegativeButton("Set", new OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

						if (maxPriceEditText.getText().length() == 0
								&& ((AreaType) areaTypeSpinner
										.getSelectedItem())
										.equals(AreaType.NoInformation)) {
							searchFragment.getFilterCheckBox()
									.setChecked(false);
						}

						if (maxPriceEditText.getText().length() == 0) {
							searchFragment.setMaxPriceFilter(false, 0.0);
						} else {
							searchFragment.setMaxPriceFilter(true, Double
									.parseDouble(maxPriceEditText.getText()
											.toString()));
						}

						if (((AreaType) areaTypeSpinner.getSelectedItem())
								.equals(AreaType.NoInformation)) {

							searchFragment.setAreaTypeFilter(false,
									AreaType.NoInformation);

						} else {
							searchFragment.setAreaTypeFilter(true,
									(AreaType) areaTypeSpinner
											.getSelectedItem());
						}

					}
				}).create();
	}

}
